/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import com.applicatioN.entities.Exiger2;
import com.applicatioN.entities.Exiger2PK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Software;
import com.applicatioN.entities.Poste;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class Exiger2JpaController implements Serializable {

    public Exiger2JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Exiger2 exiger2) throws PreexistingEntityException, Exception {
        if (exiger2.getExiger2PK() == null) {
            exiger2.setExiger2PK(new Exiger2PK());
        }
        exiger2.getExiger2PK().setNomPoste(exiger2.getPoste().getNomPoste());
        exiger2.getExiger2PK().setSoftware(exiger2.getSoftware1().getSoftware());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Software software1 = exiger2.getSoftware1();
            if (software1 != null) {
                software1 = em.getReference(software1.getClass(), software1.getSoftware());
                exiger2.setSoftware1(software1);
            }
            Poste poste = exiger2.getPoste();
            if (poste != null) {
                poste = em.getReference(poste.getClass(), poste.getNomPoste());
                exiger2.setPoste(poste);
            }
            em.persist(exiger2);
            if (software1 != null) {
                software1.getExiger2Collection().add(exiger2);
                software1 = em.merge(software1);
            }
            if (poste != null) {
                poste.getExiger2Collection().add(exiger2);
                poste = em.merge(poste);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExiger2(exiger2.getExiger2PK()) != null) {
                throw new PreexistingEntityException("Exiger2 " + exiger2 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Exiger2 exiger2) throws NonexistentEntityException, Exception {
        exiger2.getExiger2PK().setNomPoste(exiger2.getPoste().getNomPoste());
        exiger2.getExiger2PK().setSoftware(exiger2.getSoftware1().getSoftware());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger2 persistentExiger2 = em.find(Exiger2.class, exiger2.getExiger2PK());
            Software software1Old = persistentExiger2.getSoftware1();
            Software software1New = exiger2.getSoftware1();
            Poste posteOld = persistentExiger2.getPoste();
            Poste posteNew = exiger2.getPoste();
            if (software1New != null) {
                software1New = em.getReference(software1New.getClass(), software1New.getSoftware());
                exiger2.setSoftware1(software1New);
            }
            if (posteNew != null) {
                posteNew = em.getReference(posteNew.getClass(), posteNew.getNomPoste());
                exiger2.setPoste(posteNew);
            }
            exiger2 = em.merge(exiger2);
            if (software1Old != null && !software1Old.equals(software1New)) {
                software1Old.getExiger2Collection().remove(exiger2);
                software1Old = em.merge(software1Old);
            }
            if (software1New != null && !software1New.equals(software1Old)) {
                software1New.getExiger2Collection().add(exiger2);
                software1New = em.merge(software1New);
            }
            if (posteOld != null && !posteOld.equals(posteNew)) {
                posteOld.getExiger2Collection().remove(exiger2);
                posteOld = em.merge(posteOld);
            }
            if (posteNew != null && !posteNew.equals(posteOld)) {
                posteNew.getExiger2Collection().add(exiger2);
                posteNew = em.merge(posteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Exiger2PK id = exiger2.getExiger2PK();
                if (findExiger2(id) == null) {
                    throw new NonexistentEntityException("The exiger2 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Exiger2PK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger2 exiger2;
            try {
                exiger2 = em.getReference(Exiger2.class, id);
                exiger2.getExiger2PK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exiger2 with id " + id + " no longer exists.", enfe);
            }
            Software software1 = exiger2.getSoftware1();
            if (software1 != null) {
                software1.getExiger2Collection().remove(exiger2);
                software1 = em.merge(software1);
            }
            Poste poste = exiger2.getPoste();
            if (poste != null) {
                poste.getExiger2Collection().remove(exiger2);
                poste = em.merge(poste);
            }
            em.remove(exiger2);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Exiger2> findExiger2Entities() {
        return findExiger2Entities(true, -1, -1);
    }

    public List<Exiger2> findExiger2Entities(int maxResults, int firstResult) {
        return findExiger2Entities(false, maxResults, firstResult);
    }

    private List<Exiger2> findExiger2Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Exiger2.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Exiger2 findExiger2(Exiger2PK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Exiger2.class, id);
        } finally {
            em.close();
        }
    }

    public int getExiger2Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Exiger2> rt = cq.from(Exiger2.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
